/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author 013v
 */
public class CreditCardManager {

    public boolean addCreditCard(CreditCard newCredCard) {
        boolean result = false;

        try {
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            int i = 1;

            String que = "INSERT into creditcardinfo(CCfirstName, CClastName, CCCardNum, CCType, CCExpiry, "
                    + "CCBHouseNum, CCBStreet, CCBSubdivision,"
                    + "CCBCity, CCBZip, CCBCountry, AccountID) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt =
                    conn.prepareStatement(que);

            pstmt.setString(i++, newCredCard.getCCfirstName());
            pstmt.setString(i++, newCredCard.getCClastName());
            pstmt.setString(i++, newCredCard.getCCCardNum());
            pstmt.setString(i++, newCredCard.getCCType());
            pstmt.setString(i++, newCredCard.getCCExpiry());
            pstmt.setString(i++, newCredCard.getCCBHouseNum());
            pstmt.setString(i++, newCredCard.getCCBStreet());
            pstmt.setString(i++, newCredCard.getCCBSubdivision());
            pstmt.setString(i++, newCredCard.getCCBCity());
            pstmt.setString(i++, newCredCard.getCCBZip());
            pstmt.setString(i++, newCredCard.getCCBCountry());
            pstmt.setString(i++, newCredCard.getAccountID());

            pstmt.executeUpdate();
            //close DB connection
            conn.close();

            result = true;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return result;
    }

    public boolean isValidCard(CreditCard newCard, String usr) {
        boolean result = false;
        try {
           
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            String que = "SELECT * FROM creditcardinfo where CCCardNum = ? AND CCType = ? AND CCExpiry = ? AND AccountID = ?";
            PreparedStatement pstmt = conn.prepareStatement(que);

            pstmt.setString(1, newCard.getCCCardNum());
            pstmt.setString(2, newCard.getCCType());
            pstmt.setString(3, newCard.getCCExpiry());
            pstmt.setString(4, usr);
            
            ResultSet rst = pstmt.executeQuery();
            if(rst.next()){
                result = true;
            }
            
            
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public CreditCard getCard(String usr) {
        CreditCard result = null;
        try {

            String uid = new UserManager().getAccountIDFromUser(usr);

            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            String que = "SELECT * FROM creditcardinfo where AccountID=?";
            PreparedStatement pstmt =
                    conn.prepareStatement(que);

            pstmt.setString(1, uid);

            ResultSet rst = pstmt.executeQuery();
            result = new CreditCard();
            while (rst.next()) {
                result.setCCfirstName(rst.getString("CCfirstName"));
                result.setCClastName(rst.getString("CClastName"));
                //does not know if card no should be included
                result.setCCBHouseNum(rst.getString("CCBHouseNum"));
                result.setCCBStreet(rst.getString("CCBStreet"));
                result.setCCBSubdivision(rst.getString("CCBSubdivision"));
                result.setCCBCity(rst.getString("CCBCity"));
                result.setCCBZip(rst.getString("CCBZip"));
                result.setCCBCountry(rst.getString("CCBCountry"));
            }

            conn.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return result;
    }
}
